package com.example.carbon.controller;

import com.example.carbon.pojo.dto.MaterialProductUpdateInfoDto;
import com.example.carbon.pojo.vo.RestBean;
import com.example.carbon.server.info.MaterialProductServer;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/materialRelModel")
public class MaterialProductController {

    @Resource
    MaterialProductServer materialProductServer;

    /**
     * 通过模型id来获取相关联的物料档案的信息
     *
     * @param modelId
     * @return
     */
    @GetMapping("getMater/{modelId}")
    public RestBean getMaterialByModelId(@PathVariable(value = "modelId") String modelId) {
        try {
            return RestBean.success(materialProductServer.selectMaterialByModelId(modelId));
        } catch (Exception e) {
            return RestBean.failure("获取信息失败");
        }
    }

    /**
     * 更新关系
     *
     * @param modelId
     * @return
     */
    @PostMapping("update")
    public RestBean updateInfoByMaterIdModelId(@RequestBody MaterialProductUpdateInfoDto modelId) {
        try {
            if (modelId.getMaterialId().size() == 0) {
                return RestBean.success(materialProductServer.deleteInfoByModelId(modelId.getModelID()));
            } else {
                return RestBean.success(materialProductServer.insertNewInfo(modelId.getModelID(), modelId.getMaterialId()));
            }
        } catch (Exception e) {
            return RestBean.failure("服务器故障，请联系管理员");
        }
    }


}
